package com.headwind.logistics.dispatchNetwork.controller;

import com.headwind.logistics.common.pojo.BaseNetSite;
import com.headwind.logistics.common.pojo.Employee;
import com.headwind.logistics.common.util.LayuiUtil;
import com.headwind.logistics.common.util.RestResult;
import com.headwind.logistics.common.util.ResultGenerator;
import com.headwind.logistics.dispatchNetwork.dto.BaseNetSiteDto;
import com.headwind.logistics.dispatchNetwork.dto.SiteArerTreeDto;
import com.headwind.logistics.dispatchNetwork.service.inter.NetworkSiteInter;
import com.headwind.logistics.log.annotate.Log;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.*;

/**
 * @Author HUH
 * @Date 2020/7/18 15:46
 * @Version 1.0
 * @DESCRIPTION 网点控制层
 */
@Controller
public class NetworkController {
    //DAO
    @Resource
    private NetworkSiteInter networkSiteInter;

    //接口信息生成工具
    @Resource
    private ResultGenerator resultGenerator;
    /**
     * 查看所有网点 分页
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/admin/site/querySiteAll")
    @ResponseBody
    @Log("查看所有网点")
    public LayuiUtil querySiteAll(String siteName,Integer page,Integer limit){
        List<BaseNetSite> networkSiteModels = networkSiteInter.querySiteAll(siteName,page,limit);

        return LayuiUtil.data(networkSiteInter.querySiteAll(null,null,null).size(),networkSiteModels);
    }

    /**
     * 根据id查询网点
     * @param siteId
     * @return
     */
    @GetMapping("/admin/site/querySiteById")
    @ResponseBody
    @Log("根据id查询网点")
    public BaseNetSite querySiteById(Integer siteId){
        BaseNetSite networkSiteModel = networkSiteInter.querySiteById(siteId);
        return networkSiteModel;
    }

    /**
     * 树形结构(行政区域)
     * @param id
     * @return
     */
    @PostMapping("/admin/site/getData")
    @ResponseBody
    @Log("树形")
    public Object initOrgTree(Integer id){
        Sort sort = Sort.by("idx");
        List<SiteArerTreeDto> dpet = networkSiteInter.getDate(id);    //查找所有菜单
        List<HashMap<String, Object>> result = new ArrayList<>();    //定义一个map处理json键名问题
        return fun(dpet, result);
    }

    private Object fun(List<SiteArerTreeDto> dpet, List<HashMap<String, Object>> result) {
        for(SiteArerTreeDto tr : dpet){
            HashMap<String, Object> map = new HashMap<>();
            map.put("id", tr.getId());
            map.put("title", tr.getName());
            List<HashMap<String, Object>> result1 = new ArrayList<>();
            List<SiteArerTreeDto> children = tr.getChildren();    //下级菜单
            if (children != null || children.size() != 0){
                map.put("spread", true);
                map.put("children", fun(children, result1));
            }else{
                map.put("spread", false);      //设置是否展开
            }
            //这里可以根据自己需求判断节点默认选中
        /*if(m.getParent() != null || m.getChildren().size() == 0){
            map.put("checked", true);    //设置为选中状态
        }*/
            result.add(map);
        }
        return result;
    }

    @GetMapping("/admin/site/queryAllEmployee")
    @ResponseBody
    @Log("新增查询员工下拉")
    public List<Employee> queryAllEmployee(){
        List<Employee>  list= networkSiteInter.queryAllEmployee();
        return  list;
    }

    /**
     * 添加网点
     * @param baseNetSiteDto
     * @return
     */
    @PostMapping("/admin/site/addtSite")
    @ResponseBody
    @Log("新增网点")
    public RestResult addtSite(BaseNetSiteDto baseNetSiteDto){
        int count = networkSiteInter.addSite(baseNetSiteDto);

        if (count > 0 ){
            baseNetSiteDto.setSiteId(baseNetSiteDto.getSiteId());
            networkSiteInter.addApply(baseNetSiteDto);
            return resultGenerator.getFailResult("添加成功",count);
        }
        return resultGenerator.getFailResult("添加失败",count);
    }


    /**
     * 删除网点
     * @param siteId
     * @return
     */
    @PostMapping("/admin/site/deleteSite")
    @ResponseBody
    @Log("删除网点")
    public RestResult deleteSite(Integer siteId){
        int count = networkSiteInter.deleteSite(siteId);
        if(count>0){
            return resultGenerator.getSuccessResult("停用成功",count);
        }
        return resultGenerator.getFailResult("停用失败",count);
    }
}
